//
//  HYHomeHeaderView.m
//  XPlayer
//
//  Created by liguoqing on 2023/10/26.
//

#import "HYHomeHeaderView.h"
#import "HYHomeViewModel.h"
#import "HYHomeHeaderViewCell.h"

@interface HYHomeHeaderView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) HYHomeViewModel *viewModel;

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation HYHomeHeaderView

- (instancetype)initWithViewModel:(HYHomeViewModel *)viewModel
{
    self = [super init];
    if (self) {
        self.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#FFFFFF" darkModeHex:@"#181A22"];
        self.viewModel = viewModel;
        [self configUI];
        [self bindSignal];
    }
    return self;
}

- (void)configUI
{
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)bindSignal
{
    @weakify(self);
    [self.viewModel.updatedContentSignal subscribeNext:^(id x) {
        @strongify(self);
        [self.collectionView reloadData];
    }];
    [self.viewModel.reloadSubject subscribeNext:^(RACTuple *tuple) {
        @strongify(self);
        HYHomeFileType type = [tuple.first integerValue];
        if(type == HYHomeFileType_Document || type == HYHomeFileType_Photo || type == HYHomeFileType_Cloud || type == HYHomeFileType_Collect){
            NSIndexPath *indexPath = tuple.second;
            [self reloadCollect:indexPath];
        }
    }];
}

#pragma mark - UICollectionViewDelegate && UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.viewModel numberOfRowsIsHeader:YES fileType:HYHomeFileType_All];
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HYHomeHeaderViewCell *collectCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HYHomeHeaderViewCell" forIndexPath:indexPath];;
    [collectCell bindViewModel:[self.viewModel cellForRowAtIndexPath:indexPath isHeader:YES fileType:HYHomeFileType_All]];
    return collectCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.viewModel didSelectRowAtIndexPath:indexPath isHeader:YES fileType:HYHomeFileType_All];
}

- (void)reloadCollect:(NSIndexPath *)indexPath
{
    if(indexPath){
        [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
    }else{
        [self.collectionView reloadData];
    }
}

#pragma mark - loadLazy
- (UICollectionView *)collectionView
{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(kItemWidth(4), kHomeHeaderHeight);
        layout.minimumInteritemSpacing = kItemSpace;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = UIColor.clearColor;
        _collectionView.contentInset = UIEdgeInsetsMake(0, kItemSpace, 0, kItemSpace);
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[HYHomeHeaderViewCell class] forCellWithReuseIdentifier:@"HYHomeHeaderViewCell"];
    }
    return _collectionView;
}

@end
